process.root = process.env.INIT_CWD;
const { ws, requirePlugins, request } = require(`${process.root}/util`);
// 加载插件
const plugins = [...requirePlugins('plugin')];
const adminPlugins = [...requirePlugins('admin-plugin')];
const serverPlugins = [...requirePlugins('server-plugin')];

const CONFIG = require(`${process.root}/config`);
const baseUrl = CONFIG.bot.server;

// 收到消息后调用所有插件
ws.onMessage(async (data) => {
  // 过滤元数据
  const { post_type = '' } = data;
  if (post_type === 'meta_event') return;
  // 运行插件
  plugins.forEach(plugin => plugin.main({ data }))
  adminPlugins.forEach(plugin => plugin.main({ data }))
  request({ url: baseUrl + '/check', method: 'GET' }).then(() =>
    serverPlugins.forEach(plugin => plugin.main({ data }))
  ).catch(() => console.log('服务端未启动'));

});
